Release 10.1A: OpenEdge Development:
Progress Dynamics Advanced Development
Modifying the WHERE clause at run time
You can modify the base database query
WHEREclause in many ways at run time. When you link multiple SmartDataObjects together in a parent-child relationship, this modification is done automatically.Using ForeignFields to filter a dependent query
If you want to browse through
Customerrecords and then throughOrdersof a selectedCustomer, you can do this by creating separate SDOs, one forCustomerrecords and one forOrders, and then linking them together. When you do this, theOrderSDO uses a property calledForeignFieldsto modify theWHEREclause dynamically, so that theOrderSDO that was originally defined with the simple queryFOR EACH Orderis now refined to selectEACHOrder WHEREOrder.CustNum=<CustNum>.The value of the<CustNum>field is retrieved from the parentCustomerSDO each time a newCustomeris selected and plugged into theOrderquery before that query is reopened. You can define the appropriateForeignFieldsvalue either in the AppBuilder for static window procedures or in the Container Builder for dynamic windows.For the
Customer/Orderexample, the initialization code for theOrderSDO assigns the valueOrder.CustNum,CustNumto the ForeignFields property. Each time a newCustomeris selected at run time, thedataAvailableevent is published, and that event procedure in theOrderSDO queries theCustomerSDO for the current value of the foreign fieldCustNum. Note that this field refers to a field in theCustomerRowObjecttable, and therefore is not qualified by a table name (it could, in fact, be renamed from the actual database field it is derived from). This field’s value is used to set the propertyForeignValues, which holds the current values of a SmartDataObject’sForeignFields. TheWHEREclause of theOrderSDO is then modified for you to insert the phraseOrder.CustNum = <CustNum>, where<CustNum>is filled in from theForeignValuesproperty, and the query is prepared and opened. A dependent query can contain multiple foreign fields, and the values for them are kept in sync with the list of fields that make up the foreign key.The functions setQueryWhere and setQuerySort
The earliest versions of the Version 9 SmartObjects supported two basic functions to manipulate the
WHEREclause:setQueryWhereandsetQuerySort. These work well in straightforward cases, butsetQueryWhere, in particular, does not deal well with successive changes to theWHEREclause or other more complex needs. Therefore, we do not recommend that you usesetQueryWherein new application code. ThesetQuerySortfunction can be used to change the sort sequence of a query, but remember that any query that has aWHEREclause or other filter applied to it is sorted automatically based on the indexes used to satisfy the query, and this is typically the most appropriate sort sequence.If you want to allow users to sort a result set in different ways after it has been retrieved, you can do this more efficiently by manipulating the temp-table query instead of sorting and reopening the database query, as we discuss later in this section.
Instead, you should generally use the
addQueryWhereandassignQuerySelectionfunctions described below. These are much more flexible and can operate more efficiently.The resortQuery function
The
resortQuery()function takes an existing query and resorts it according to criteria you specify. For example:
The setBaseQuery function
There is an additional function that can be of use in some cases, if you want to set a basic filter on a dataset so that it cannot be lost by changes to the
Note: SDOs do not support the use of this function to change the database tables the query operates on.WHEREclause later on. The property that stores the base query isBaseQuery, whose initial value is the query defined when the SDO is created. To apply a filter at run time that is not removed by other changes, you can reset this property, effectively overriding the basic definition of the SDO’s query in that instance. In the following example, we have runsetOpenQueryto change the basic query to return onlyCustomerrecords where the State field equals MA. All calls to the otherWHEREclause functions then append theirWHEREclause to this new basic query definition. Note that because we’re resetting the entireOpen Querystatement, we need to specify the query starting withFOREACH.You can override another default behavior of the SDO, which is to open its database query as soon as the SDO is initialized. You might not want to wait for that to happen if the SDO is always filtered before the data is actually used. In this case, you can reset the
OpenOnInitproperty of the SDO mentioned earlier in this chapter toFALSEto cause it to wait until theopenQueryfunction is run after initialization.
|
Copyright © 2005 Progress Software Corporation www.progress.com Voice: (781) 280-4000 Fax: (781) 280-4095 |